﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using BaseSet.Entity.Common.DataOperation;

namespace WebSite.Admin.CompanyManager
{
    public partial class CompanyList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(CompanyUPager);

            LoadCondition();

            string companyname = txtCompanyName.Text.Trim();

            //List<Member> list = new List<Member();
            CompanysHelper chelper = new CompanysHelper();
            Criteria c = new Criteria();

            if (!string.IsNullOrEmpty(companyname))
            {
                c.Add(CriteriaType.Like, "CompanyName", "%" + companyname + "%");
            }

            CompanyUPager.ItemCount = chelper.QueryCompanysCountBySearch(c);

            // 以下的值从BasePage中得到
            CompanyUPager.UrlFormat = UrlFormat;
            CompanyUPager.PrefixText = GetPreFixText(CompanyUPager);

            if (mark)
            {
                PagerInit(CompanyUPager);
            }

            string[] fields = new string[] { "ID", "CompanyName", "Brief", "UserNum", "CareNum", "BlogPVNum", "Flag","CreateDate" };

            DataTable dt = new DataTable();
            dt = chelper.QueryCompanysBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, CompanyUPager.Begin - 1, CompanyUPager.Count, fields);

            dt.Columns.Add(new DataColumn("strFlag"));
            dt.Columns.Add(new DataColumn("strCreateDate"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string getflag = dr["flag"] + "";
                    string strflag = "";
                    if (getflag == "0")
                    {
                        strflag = "启用";
                    }
                    else if (getflag == "9")
                    {
                        strflag = "禁用";
                    }
                    dr["strflag"] = strflag;

                    DateTime dtCreateDate = Convert.ToDateTime(dr["createdate"]);
                    dr["strCreateDate"] = dtCreateDate.ToString("yyyy-MM-dd");
                }
            }

            rptCompany.DataSource = dt;
            rptCompany.DataBind();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }

        #region 查询条件设置
        private void SetCondition()
        {
            string companyname = txtCompanyName.Text.Trim();

            //将查询条件放入Session中
            Session["QueryList"] = companyname + "@" + "CompanyList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 2)
            {
                string page = conditionAll[1];
                if (page == "CompanyList")
                {
                    txtCompanyName.Text = conditionAll[0];
                }
            }
        }
        #endregion

        #region 列表事件
        protected void rptCompany_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");

            string id = hidID.Value;

            if (e.CommandName == "close")//禁用
            {
                Companys cinfo = new Companys();
                CompanysHelper chelper = new CompanysHelper();
                cinfo = chelper.GetCompanys(id);

                cinfo.Flag = 9;
                chelper.EditCompanys(cinfo);

                Tools.Alert("小组禁用成功！");
            }
            else if (e.CommandName == "open")//开启
            {
                Companys cinfo = new Companys();
                CompanysHelper chelper = new CompanysHelper();
                cinfo = chelper.GetCompanys(id);

                cinfo.Flag = 0;
                chelper.EditCompanys(cinfo);

                Tools.Alert("小组开启成功！");
            }

            LoadContent(false);
        }
        #endregion
    }
}